package inet.app.controller.database;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class DaoBase {
	
	protected String tag = "DaoBase";
	protected Connection dbConnection;
	protected Context context;

	/**
	 * constructor
	 */
	public DaoBase(Context context) {
		this.context = context;
		dbConnection = Connection.getConnection(context);
		tag = getClass().getSimpleName();
	}

	protected int deleteAll(String table) {
		int res = -1;
		if (!dbConnection.openSession()) {
			return res;
		}
		res = dbConnection.delete(table, null, null);
		dbConnection.closeSession();
		return res;
	}

	protected int deleteAll(String table, String whereClause, String[] args) {
		int res = -1;
		if (!dbConnection.openSession())
			return res;
		res = dbConnection.delete(table, whereClause, args);
		dbConnection.closeSession();
		return res;
	}

	protected long insertRow(String table, ContentValues values) {
		if (!dbConnection.openSession())
			return -1;
		long insertId = dbConnection.insert(table, null, values);
		dbConnection.closeSession();
		return insertId;
	}

	protected long insertCollection(String table, List<ContentValues> values) {
		if (!dbConnection.openSession())
			return -1;
		long insertId = dbConnection.insert(table, null, values);
		dbConnection.closeSession();
		return insertId;
	}
	
	public boolean isExistData(String nameTable) {
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return false;
			cursor = dbConnection.query(DBConfig.TABLE_KQGIAI_REGION, null,
					null, null, null, null, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					return true;
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return false;
	}
}
